﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VSDBM.Code.functions;
using System.Threading;

using VSDBM.Code.dto;


public partial class Visual_Procedures_Alignments_SequenceAlignment : System.Web.UI.Page
{
    bool done = false;
    String texto = "";
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void alinharNeedleman()
    {
        DateTime start = DateTime.Now;
        String seqf = "$" + tbseqfirst.Text;
        String seqs = "$" + tbseqsec.Text;
        Needlemanwunsch ali = new Needlemanwunsch(seqs.ToCharArray(), seqf.ToCharArray());

        ali.executar();
        String elapsed = DateTime.Now.Subtract(start).ToString(@"hh\:mm\:ss\.FF");
        tbresult.Text = "Score: " + ali.mScore + "\r\n" + "Time elapsed: " + elapsed + "\r\n" + ali.mAlignmentSeqA + "\r\n" + ali.mAlignmentSeqB;

    }
    protected void alinharNeedlemanMMF()
    {
        DateTime start = DateTime.Now;
        String seqf = "$" + tbseqfirst.Text;
        String seqs = "$" + tbseqsec.Text;
        NeedlemanWunschMMF ali = new NeedlemanWunschMMF(seqs.ToCharArray(), seqf.ToCharArray());

        ali.executar();
        String elapsed = DateTime.Now.Subtract(start).ToString(@"hh\:mm\:ss\.FF");
        tbresult.Text = "Score: " + ali.mScore + "\r\n" + "Time elapsed: " + elapsed + "\r\n" + ali.mAlignmentSeqA + "\r\n" + ali.mAlignmentSeqB;

    }

    public void tAlinhamentof(object ali)
    {
        DateTime start = DateTime.Now;
        Needlemanwunsch alin = ali as Needlemanwunsch;
        alin.executar();
        String seqorign = new string(alin.mSeqB);
        String texto = seqorign.Replace("\r\n", "").Replace("$", "").ToUpper() + "\r\n" + alin.mAlignmentSeqA.Replace("\r\n", "").Replace("$", "").ToUpper();
        this.texto = texto;
        this.texto += "  SCORE-> " + Convert.ToString(alin.mScore);
        String elapsed = DateTime.Now.Subtract(start).ToString(@"hh\:mm\:ss\.FF");
        this.texto += " TIME ELAPSED-> " + elapsed;
        
        done = true;

    }
    
    protected void alinharWaterman()
    {
        DateTime start = DateTime.Now;
        Sequence database = new Sequence();
        Sequence query = new Sequence();
        database.sequence = tbseqsec.Text.ToUpper().Replace("\r\n", "").Replace(" ", "");
        query.sequence = tbseqfirst.Text.ToUpper().Replace("\r\n", "").Replace(" ", "");
        SmithWatermanMemory swdb = new SmithWatermanMemory(database, query, new SubstitutionMatrix(),true);
        if (swdb.alignment == null)
        {
            tbresult.Text = "NO ALIGNMENT FOUND!";
        }
        else
        {
            String elapsed = DateTime.Now.Subtract(start).ToString(@"hh\:mm\:ss\.FF");
            tbresult.Text = "Alinhamento:";
            tbresult.Text += "\r\n";
            tbresult.Text += swdb.alignment.ToString();
            tbresult.Text += "\r\n";
            tbresult.Text += "Best Alignment Score:";
            tbresult.Text += swdb.highScore.ToString();
            tbresult.Text += "\r\n";
            tbresult.Text += "Time elapsed: "+elapsed;
        }
    }
    protected void alinharWatermanMMF()
    {
        DateTime start = DateTime.Now;
        Sequence database = new Sequence();
        Sequence query = new Sequence();
        database.sequence = tbseqsec.Text.ToUpper().Replace("\r\n", "").Replace(" ", "");
        query.sequence = tbseqfirst.Text.ToUpper().Replace("\r\n", "").Replace(" ", "");
        SmithWatermanMMF swdb = new SmithWatermanMMF(database, query, new SubstitutionMatrix(),true);
        if (swdb.alignment == null)
        {
            tbresult.Text = "NO ALIGNMENT FOUND!";
        }
        else
        {
            String elapsed = DateTime.Now.Subtract(start).ToString(@"hh\:mm\:ss\.FF");
            tbresult.Text = "Alinhamento:";
            tbresult.Text += "\r\n";
            tbresult.Text += swdb.alignment.ToString();
            tbresult.Text += "\r\n";
            tbresult.Text += "Best Alignment Score:";
            tbresult.Text += swdb.highScore.ToString();
            tbresult.Text += "\r\n";
            tbresult.Text += "Time elapsed: " + elapsed;
        }
    }

   
    protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        if (rdlOption.SelectedValue == "1")
        {
            alinharNeedleman();
        }
        else if (rdlOption.SelectedValue == "2")
        {
            alinharWatermanMMF();
        }
        else if (rdlOption.SelectedValue == "3")
        {
            alinharWaterman();
        }
        else if (rdlOption.SelectedValue == "4")
        {
            alinharNeedlemanMMF();
        }
        



    }

}